package com.lcz.musicplay.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lcz.musicplay.R;
import com.lcz.musicplay.domain.Music;
import com.lcz.musicplay.utils.MediaUtils;

/**
 * Created by hp on 2016/8/19.
 */
public class MyAdapter extends BaseAdapter {

    private Context context;

    public MyAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        if (MediaUtils.musicList != null) {
            return MediaUtils.musicList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        if (MediaUtils.musicList != null) {
            return MediaUtils.musicList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;
        ViewHolder holder;
        if (v == null) {
            v = View.inflate(context, R.layout.item_musiclist, null);
            holder = new ViewHolder();
            holder.title = (TextView) v.findViewById(R.id.tv_title);
            holder.artist = (TextView) v.findViewById(R.id.tv_artist);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        Music music = MediaUtils.musicList.get(position);
        holder.title.setText(music.getTitle());
        holder.artist.setText(music.getArtist());

        /*设置选中后的文本颜色*/
        if (MediaUtils.CURPOSION == position) {
            holder.title.setTextColor(Color.GREEN);
        } else {
            holder.title.setTextColor(Color.WHITE);
        }
        //标记存储被选中的位置
        holder.title.setTag(position);
        return v;
    }

    class ViewHolder {
        TextView title;
        TextView artist;
    }
}
